import { createRouter, createWebHashHistory } from 'vue-router'
import { routes } from './routes'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to) => {
  const store = useUserStore()
  const whiteList = ['/login', '/register']

  // 先检查是否在白名单中
  if (whiteList.includes(to.path)) {
    return true
  }

  // 检查是否有token
  if (!store.token) {
    return '/login'
  }

  return true
})

export default router
